package org.msn.gui.controller;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
import org.msn.component.SelfMessageHBox;
import org.msn.config.CloseInfo;
import org.msn.config.MessageInfo;
import org.msn.config.RoomInfo;
import org.msn.gui.constant.APIConstant;
import org.msn.gui.init.MsnInit;

import java.io.IOException;

public class MsnController extends MsnInit {
    @FXML
    private TextArea msg;

    public void showAllRoom(MouseEvent event) throws IOException {
        // 如果有房间名
        if (!hasRoomName()) {
            return;
        }

        // 查看房间前需要查看是否拥有用户名
        getUsername();

        if (hasUsername()) {
            return;
        }

        // 发起请求
        sendGetAllRoom(ctx, null);

        Stage stage = new Stage();
        Parent root = FXMLLoader.load(getClass().getResource("/fxml/showAllRoomFxml.fxml"));
        stage.setScene(new Scene(root,
                (Integer) APIConstant.CONFIG.get("width"),
                (Integer) APIConstant.CONFIG.get("height")));

        exportShowAllRoomStage = stage;

        stage.show();
    }

    public void sendMsg(MouseEvent mouseEvent) throws IOException {
        // 查看是否拥有房间名 没有房间名不能发送消息
        if (hasRoomName()) {
            showAllRoom(mouseEvent);
            return;
        }

        // 输出消息到消息面板
        chatPane.getChildren().add(new SelfMessageHBox((String) APIConstant.CONFIG.get("name"), msg.getText()));

        // 发起请求
        sendSendMsg(ctx, MessageInfo
                .builder()
                .roomName(MsnInit.exportRoomName.getText())
                .username((String) APIConstant.CONFIG.get("name"))
                .msg(msg.getText())
                .build());

        // 清除用户输入消息面板
        msg.clear();
    }

    public void openVideo(ActionEvent actionEvent) {
        sendOpenVideo(ctx, RoomInfo.builder().name(MsnInit.exportRoomName.getText()).build());
    }

    public void openSound(ActionEvent actionEvent) {
        sendOpenSound(ctx, RoomInfo.builder().name(MsnInit.exportRoomName.getText()).build());
    }

    public void leaveRoom(MouseEvent mouseEvent) {
        // 如果没有房间名
        if (hasRoomName()) {
            return;
        }

        // 发起请求
        sendLeaveRoom(ctx, CloseInfo
                .builder()
                .roomName(MsnInit.exportRoomName.getText())
                .build());

        MsnInit.exportChatPane.getChildren().clear();
        MsnInit.exportRoomName.setText("未加入房间");
    }

    /**
     * 得到用户的姓名
     */
    private void getUsername() throws IOException {
        if (!hasUsername()) {
            return;
        }

        Stage stage = new Stage();
        Parent root = FXMLLoader.load(getClass().getResource("/fxml/getUsernameFxml.fxml"));
        stage.setScene(new Scene(root, 200, 150));
        stage.show();

        exportGetUsernameStage = stage;
    }

    private boolean hasRoomName() {
        return MsnInit.exportRoomName.getText().equals("未加入房间");
    }

    private boolean hasUsername() {
        return APIConstant.CONFIG.get("name") == null;
    }
}
